import api from '@/api';

export default {
  namespace: 'user',

  state: {
    subUsers: {
      list: [],
      pagination: {},
    },
    currentUser: {},
    enterprise: {},
    userList: {
      list: [],
      pagination: {},
    },
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(api.user.querySubUsers, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *fetchUsers({ payload }, { call }) {
      return yield call(api.user.queryUsers, payload);
    },
    *fetchCurrent({ payload }, { call, put }) {
      const response = yield call(api.user.queryCurrent, payload);
     
      if (response.code === 200) {
        yield put({
          type: 'saveCurrentUser',
          payload: response.data,
        });
      }
    },
    *fetchEnterprise(_, { call, put }) {
      const response = yield call(api.user.queryEnterprise);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveEnterprise',
          payload: response.data,
        });
      }
    },
    *fetchByOrg({ payload }, { call }) {
      return yield call(api.user.queryByOrg, payload);
    },
    *queryUserList({ payload }, { call, put }) {
      const response = yield call(api.org.queryOrgUsers, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveUser',
          payload: response,
        });
      }
    },
    *add({ payload }, { call, put }) {
      return yield call(api.user.userManage.create, payload);
    },
    *update({ payload }, { call, put }) {
      return yield call(api.user.userManage.update, payload);
    },
    *delete({ payload }, { call }) {
      return yield call(api.user.userManage.remove, payload);
    },
    *lock({ payload }, { call, put }) {
      return yield call(api.user.userManage.lock, payload);
    },
    *unlock({ payload }, { call, put }) {
      return yield call(api.user.userManage.unlock, payload);
    },
    *resetPassword({ payload }, { call, put }) {
      return yield call(api.user.userManage.resetPassword, payload);
    },
    *queryUserListByKeywords({ payload }, { call, put }) {
      return yield call(api.user.userManage.queryUserList, payload);
    },
    *queryRoles({ payload }, { call, put }) {
      return yield call(api.user.queryRoles, payload);
    },
    *querySmsPermission({ payload }, { call, put }) {
      return yield call(api.user.querySmsPermission, payload);
    },
  },

  reducers: {
    save(state, action) {
      const { data, pagination } = action.payload;
      return {
        ...state,
        subUsers: {
          list: data,
          pagination,
        },
      };
    },
    saveCurrentUser(state, action) {
      return {
        ...state,
        currentUser: action.payload || {},
      };
    },
    saveEnterprise(state, action) {
      return {
        ...state,
        enterprise: action.payload || {},
      };
    },
    changeNotifyCount(state, action) {
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
          notifyCount: action.payload,
        },
      };
    },
    saveUser(state, action) {
      const { data, pagination } = action.payload;
      return {
        ...state,
        userList: {
          list: data,
          pagination,
        },
      };
    },
  },
};
